﻿Class DefaultContentText(Of T As {New, IContent})
    Implements IContentText

    Private _content_type As ContentType
    Private _write_properties As IEnumerable(Of ContentProperty)
    Private _reference_properties As IEnumerable(Of ContentReference)

    Sub New()
        _content_type = getMystery().AssemblyRegister.getMysteryClassAttribute(Of ContentType, T)()

        Dim all As IEnumerable(Of ContentProperty) = getMystery().AssemblyRegister.getMysteryPropertyAttributes(Of ContentProperty, T)()
        _write_properties = New HashSet(Of ContentProperty)((From x As ContentProperty In all Select x Where x.retrive IsNot Nothing AndAlso x.used_in.getMysteryAttribute(Of PropertyTexterAttr)() IsNot Nothing))
        Dim references As IEnumerable(Of ContentReference) = getMystery.AssemblyRegister.getMysteryPropertyAttributes(Of ContentReference, T)()
        _reference_properties = New HashSet(Of ContentReference)((From x As ContentReference In references Select x Where x.retrive IsNot Nothing AndAlso x.used_in.getMysteryAttribute(Of PropertyTexterAttr)() IsNot Nothing))

    End Sub

    Public Function generate(item As IContent) As String Implements IContentText.generate
        Dim result As String = String.Empty

        For Each wp As ContentProperty In _write_properties
            Dim pv As ContentPropertyValue = wp.retrive(item)
            Dim value As Object = If(pv Is Nothing, "", pv.getValue())
            If value Is Nothing Then value = ""

            'I have dates , when the hell they fix it, date.min is out of range in sql
            If TypeOf (value) Is Date AndAlso value = Date.MinValue Then
                value = ""
            Else
                value = value.ToString
            End If
            result &= wp.label & ":" & vbNewLine
            result &= value.ToString & vbNewLine & vbNewLine
        Next


        'references

        For Each rp As ContentReference In _reference_properties

            Dim pv As BaseContentReference = rp.retrive()(item)

            result &= rp.label & ":" & vbNewLine

            Dim strings As New HashSet(Of String)
            For Each ref As IContent In pv.getAsEnum()
                strings.Add(ref.ToString)
            Next


            result &= String.Join(", ", strings) & vbNewLine & vbNewLine

        Next


        Return result

    End Function




End Class

